<?php
namespace app\index\controller;
use \think\controller;
use think\Db;
use app\index\model\Haven;
class MysqlData extends Base{
//    protected $beforeActionList = array(
//        'ConnectDatabase' => '',
//    );
//    protected $link = '';
//    protected function ConnectDatabase(){
//        $this ->link = Db::connect();
//    }
    public function connect(){
//        $result = Db::name('haven') ->whereOr(array('fir_cn_letter'=>'D','fir_ue_letter'=>'C')) ->where(array('is_hot'=>1,'status'=>array('neq',9)))  ->column('id','cn_name');
//        var_dump($result);
//        $haven = new Haven();
//        $result = $haven ->where(array('is_hot'=>1,'status'=>array('neq',9))) ->find() ->getData();
//        var_dump(Haven::get('9')->getData());
//        $add = DB::insert(array('k'=>'v','key'=>'value','keys'=>'values'));
//        $addAll = DB::insertAll(array(
//            array('k'=>'v','key'=>'value','keys'=>'values'),
//            array('k'=>'v','key'=>'value','keys'=>'values'),
//            array('k'=>'v','key'=>'value','keys'=>'values'),
//        ));


    }

    public function insert(){
//        可以单条添加也可以多条添加 ——看看能不能多条添加
        $data['fir_cn_letter'] = 'Z';
        $data['fir_ue_letter'] = 'Z';
        $data['cn_name'] = '中华人民共和国';
        $data['ue_name'] = 'ZhongHuaRenMinGongHeGuo';
        $data['is_hot'] = '1';
        $data['create_time'] = '2019-01-01';
        $res = Haven::create($data);
        var_dump($res);
    }

    public function update(){
        $data['is_hot'] = 0;
        $data['status'] = 1;
        $data['update_time'] = time();
        $where['id'] = '10';
//        更新数据，更新条件，可更新字段
        $res = Haven::update($data, $where);
        var_dump($res);
    }

    public function select(){
//        $id = 10;
        $where = function($query){
            $query -> where(array('is_hot'=>0,'status'=>array('neq',9)))-> field('id, cn_name, is_hot, create_time')-> order('create_time desc');
        };
        $res = Haven::select($where);
        foreach($res as $k => $v){
//            var_dump($res ->getData());
//            var_dump(($k+1).'=>'.$v['cn_name']);
            var_dump($v ->create_time);
        }

//        $result = Db::name('haven') ->whereOr(array('fir_cn_letter'=>'D','fir_ue_letter'=>'C')) ->where(array('is_hot'=>1,'status'=>array('neq',9)))  ->select();
//        var_dump($res ->getdata());
    }

    public function delete(){
        $where = function($query){
            $query -> where(array('is_hot'=>0,'status'=>array('neq',9)));
        };
        $res = Haven::get($where);
//        var_dump($res);
//        dump(Haven::destroy($where));
    }
}